﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Xml;
using System.Globalization;
/// <summary>
/// Summary description for LocalFunctions
/// </summary>
/// 
public enum CalendarPageType { AllEvents , Competition , ByMonth};

public class LocalFunctions
{
    private Translations Lang = new Translations();
    public string pAlignment;

    public Int32 LangID;

    public string sSelected = "color: #FFFFFF; background-image: url(../images/bullet3.png)";
    public string sSelectedHeb = "color: #FFFFFF; background-image: url(../images/bullet3H.png)";

    public string FullAppPath
    {
        get
        {
            //Return variable declaration
            string appPath = null;

            //Getting the current context of HTTP request
            HttpContext context = HttpContext.Current;

            //Checking the current context content
            if (context != null)
            {
                //Formatting the fully qualified website url/name
                appPath = string.Format("{0}://{1}{2}{3}",
                context.Request.Url.Scheme,
                context.Request.Url.Host,
                (context.Request.Url.Port == 80) ? string.Empty : ":" + context.Request.Url.Port,
                context.Request.ApplicationPath);
            }

            return appPath;
        }
    }

	public LocalFunctions()
	{
        Int32 iTmpLang = 0;
        Int32.TryParse(HttpContext.Current.Request.QueryString["LangID"], out iTmpLang);
        if (iTmpLang > 0)
        {
            LangID = iTmpLang;
            SetLangID(LangID);
        }
        else
        {
            LangID = GetLangID();
        }

        switch (LangID)
        {
            default:
            case 1:
                Lang = new Translations("ru", 0);
                pAlignment = "ltr";
                break;

            case 2:
                Lang = new Translations("he", 0);
                pAlignment = "rtl";
                break;
        }
	}

    public LocalFunctions(bool isAdmin)
    {

    }

    public string GetMonthName(DateTime givenDate)
    {
        if (LangID == 1)
            return CultureInfo.GetCultureInfo("ru-RU").DateTimeFormat.GetMonthName(givenDate.Month);
        else
            return CultureInfo.GetCultureInfo("he-IL").DateTimeFormat.GetMonthName(givenDate.Month);
    }

    public void SetLangID(Int32 iLangID)
    {
        HttpCookie langCoockie = new HttpCookie("LangID");
        langCoockie.Value = iLangID.ToString();
        HttpContext.Current.Response.SetCookie(langCoockie);
        HttpContext.Current.Session["LangID"] = iLangID.ToString();
    }

    private Int32 GetLangID()
    {
        Int32 iLangID =1;
        if (HttpContext.Current.Session["LangID"] == null)
        {
            if (HttpContext.Current.Request.Cookies["LangID"] != null)
            {
                iLangID = Convert.ToInt32(HttpContext.Current.Request.Cookies["LangID"].Value);
                if (iLangID == 0)
                    iLangID = 1;
            }
            HttpContext.Current.Session["LangID"] = iLangID.ToString();
        }
        else
        {
            iLangID = Convert.ToInt32(HttpContext.Current.Session["LangID"]);
        }
        return iLangID;
    }

    public String GetPhrase(Int32 iNum)
    {
        String iRetValue = String.Empty;
        for (Int32 i = 0; i < Lang.XMLdata.Values.Length; i++)
            if (Lang.XMLdata.Values[i].key == iNum.ToString())
            {
                iRetValue = Lang.XMLdata.Values[i].name;
                break;
            }
        return iRetValue;

    }
}
